package com.tianhua.javs.core.func.script;

import com.tianhua.javs.client.bean.JavsSegmentBean;
import com.tianhua.javs.client.bean.ScriptVarMappingBean;
import com.tianhua.javs.client.bean.TranslateResultBean;
import com.tianhua.javs.client.func.IScriptFunc;
import com.tianhua.javs.client.func.ITranslatorFunc;
import com.tianhua.javs.core.func.TranslatorContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Description:将对Java原生类对应的函数进行缩写
 * 比如smap: stream().map(....
 * cl(): collect(Collectors.toList())
 * date: 2022/2/12
 *
 * @author shenshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
@Component(value = "javaSDKFunc")
public class JavaSDKFunc implements IScriptFunc {

    @Autowired
    private TranslatorContainer translatorContainer;

    @Override
    public TranslateResultBean translate(String keyWardStr, Map<String, List<ScriptVarMappingBean>> javsVarMap) {
        Component component = JavaSDKFunc.class.getAnnotation(Component.class);
        List<ITranslatorFunc> translatorFuncList = translatorContainer.getByScriptFuncBean(component.value());
        String trimSegment = keyWardStr.trim();
        for (ITranslatorFunc translatorFunc : translatorFuncList){
            JavsSegmentBean javsSegmentBean = translatorFunc.translate(trimSegment);
            if(Objects.isNull(javsSegmentBean)){
               continue;
            }
            trimSegment = javsSegmentBean.getSegment();
        }
        return TranslateResultBean.getInstance(trimSegment);
    }
}
